package com.ruoyi.project.wework.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.model.AjaxResult;
import com.ruoyi.project.wework.ConfigProperties;
import com.ruoyi.project.wework.manager.WeworkManager;
import com.ruoyi.project.wework.request.SyncRequest;
import com.ruoyi.project.wework.request.WeworkLoginRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 */
@RestController
@RequestMapping("/wework")
public class WeworkController extends BaseController {
    @Autowired
    private WeworkManager weworkManager;

    @GetMapping("/notice")
    public AjaxResult list() {

        return AjaxResult.success("TODO");
    }

    @GetMapping("/find-config")
    public AjaxResult findConfig() {
        ConfigProperties config = weworkManager.findWeworkConfig();
        return AjaxResult.success(config);
    }

    @PostMapping("/sync-dept")
    public AjaxResult syncDept(@RequestBody SyncRequest request) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        weworkManager.syncDept(request, operatorId, tenantId);
        return AjaxResult.success("同步中，请稍后刷新页面...");
    }

    @PostMapping("/sync-user")
    public AjaxResult syncUser(@RequestBody SyncRequest request) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        weworkManager.syncUser(request, operatorId, tenantId);
        return AjaxResult.success("同步中，请稍后刷新页面...");
    }


    @GetMapping("/refresh-accesstoken")
    public AjaxResult refreshAccesstoken() {
        weworkManager.refreshAccessToken();
        return AjaxResult.success();
    }

    @PostMapping("/welogin")
    public AjaxResult welogin(@RequestBody WeworkLoginRequest request) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = weworkManager.simpleLogin(request);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }


}
